﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ProxyHero.Common;
using System.Text.RegularExpressions;
using ProxyHero.Loading;
using System.Threading;

namespace ProxyHero.TabPages
{
    public partial class frmCheck : Form
    {
        delegate void DelegateSetStatus(object text);
        Thread threadGetLocation, threadCheckAnouymous;
        public frmCheck()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 查询类型
        /// </summary>
        /// <param name="type"></param>
        public frmCheck(string type)
        {
            InitializeComponent();
            this.Text = type;
            switch (this.Text)
            {
                case "IP地理位置查询":
                    this.lblIp.Text = "IP地址："; break;
                case "代理匿名度查询":
                    this.lblIp.Text = "代理服务器："; break;
                default:
                    this.lblIp.Text = "IP地址："; break;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtbResult.Clear();
                if (this.lblIp.Text == "IP地址：")
                {
                    threadGetLocation = new Thread(new ThreadStart(this.GetLocation));
                    threadGetLocation.Start();
                }
                else if (this.lblIp.Text == "代理服务器：")
                {
                    threadCheckAnouymous = new Thread(new ThreadStart(this.CheckAnonymous));
                    threadCheckAnouymous.Start();
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void SetStatus(object text)
        {
            this.tsslStatus.Text = text.ToString();
        }

        private void GetLocation()
        {
            DelegateSetStatus dss = new DelegateSetStatus(this.SetStatus);
            this.Invoke(dss, new object[] { "正在查询地理位置..." });
            Regex regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))");
            MatchCollection matchs = regex.Matches(this.txtIP.Text.Trim());
            if (matchs.Count == 1)
            {
                IpHelper ipHepler = new IpHelper(this.txtIP.Text.Trim());
                this.rtbResult.Text = ipHepler.IpAddress + "：" + ipHepler.Location;
            }
            this.Invoke(dss, new object[] { "就绪" });
        }

        private void CheckAnonymous()
        {
            DelegateSetStatus dss = new DelegateSetStatus(this.SetStatus);
            this.Invoke(dss, new object[] { "正在验证代理匿名度..." });
            IpHelper ipHepler = new IpHelper(this.txtIP.Text.Trim());
            this.rtbResult.Text = ipHepler.ProxyType;
            this.Invoke(dss, new object[] { "就绪" });
        }

        private void frmCheck_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (threadCheckAnouymous != null && threadCheckAnouymous.IsAlive)
            {
                threadCheckAnouymous.Abort();
                threadCheckAnouymous.Join();
            }

            if (threadGetLocation != null && threadGetLocation.IsAlive)
            {
                threadGetLocation.Abort();
                threadGetLocation.Join();
            }
        }
    }
}
